<?php
/**
 * 此文件是接口程序路由
 * 
 * @package class
 * @author Jerry
 * @version $Id: controller.class.php 5683 2009-02-05 09:57:52Z jerry $
 */


/**
 * 程序路由处理类
 *
 * 这里类判断外界参数调用内部方法
 */
class Application
{
	var $controller 		= null;  		//类名
	var $action				= null;  		//方法名
	var $default_controller = null;   		//默认的类名
	var $default_do         = null;     	//默认的方法名
	var $controller_var     = 'c';  		//Post or Get 控制器变量
	var $action_var         = 'd';			//Post or Get 函数变量

	/**
	 * 设置默认的类名
	 *
	 * @param string $default_controller
	 */
	function setDefaultController($default_controller)
	{
		$this->default_controller = $default_controller;
	}
	
	/**
	 * 设置默认的方法名
	 *
	 * @param string $default_action
	 */
	function setDefaultAction($default_action)
	{
		$this->default_action = $default_action;
	}
	
	/**
	 * 调用实际类和方式
	 *
	 */
	function run()
	{
		global $in_var;
		
		$this->controller = $in_var[$this->controller_var] ? $in_var[$this->controller_var] : $this->default_controller;
		$this->action     = $in_var[$this->action_var] ? $in_var[$this->action_var] : $this->default_action;

		if(!$this->controller || !$this->action) throw new AppException('The lack of parameters');
		
		$class_file = CONTROLLER_DIR . $this->controller . '.class.php'; 

		if(!is_file($class_file))
        {
        	throw new AppException('Invalid controller ' . $this->controller);
        }

		require_once $class_file;
		
		$class = ucfirst($this->controller);
		
		if(!class_exists($class))
		{
			throw new AppException('Invalid Class ' . $class);
		}
		
		$instance = new $class();
		
		$function = $this->action;
		
		if(!method_exists($instance,$function))
		{
			throw new AppException('Invalid Function ' . $function);
		}
		
		$instance->$function();
	}
}


/**
 * $Log$
 */
?>